﻿
using Avalonia;
#pragma warning disable CA1816
namespace KinonekoSoftware.Platform.Controls
{
    partial class DialogHost
    {
        
        public class BusySession : IBusyService
        {
            internal BusySession(DialogHost host)
            {
                Host = host;
            }

            public void Update(string text)
            {
                Xplat.RunOnUIThread(() =>
                {
                    Host.IsBusy   = true;
                    Host.BusyText = text;
                });
            }
            
            public void Dispose()
            {
                if (Host is null)
                {
                    return;
                }
                
                Xplat.RunOnUIThread(() =>
                {
                    Host.BusyText = string.Empty;
                    Host.IsBusy   = false;
                });
            }
            
            public DialogHost Host { get; }
            public Task Await(int t = 1)
            {
                return Task.Delay(100 * Math.Clamp(t, 1, 30));
            }
        }
        
        private BusySession _session;
        
        public IBusyService BeginSession(string text)
        {
            _session ??= new BusySession(this);
            _session.Update(text);
            return _session;
        }


        public void EndSession()
        {
            //
            _session?.Dispose();
        }
    }
}
#pragma warning restore CA1816